#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

void test_vector1()
{
	vector<int>v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);

	for (int i = 0; i < 4; i++) cout << v1[i] << " ";
	cout << endl;

	//迭代器遍历
	vector<int>::iterator it1 = v1.begin();
	while (it1 != v1.end())
	{
		cout << *it1 << " ";
		++it1;
	}
	cout << endl;

	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;
}

void test_vector2()
{
	vector<string> v2;
	string s1("张三");
	v2.push_back(s1);
	v2.push_back(string("李四"));
	v2.push_back("王五");

	v2[1] += "来";

	for (const auto& e : v2) //最好加引用注意一下
	{
		cout << e << " ";
	}
	cout << endl;
}

void test_vector3()
{
	vector<int>v1;
	v1.push_back(1);
	v1.push_back(10);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(21);
	v1.push_back(8);
	v1.push_back(4);

	//默认为升序
	//sort(v1.begin(), v1.end());
	//sort(v1.begin() + 1, v1.end() - 1);
	//sort(v1.begin(), v1.begin() + v1.size()/ 2);

	////改成降序
	//greater<int>gt; //支持反函数的比较
	//cout << gt(2, 3) << endl;
	//cout << gt.operator()(2, 3) << endl; //上下等价
	//cout << gt(3, 2) << endl;
	//sort(v1.begin(), v1.end(),gt);

	sort(v1.begin(), v1.end(), greater<int>());

	for (const auto& e : v1)
	{
		cout << e << " ";
	}
	cout << endl;
}


void test_vector4() //插入
{
	vector<int>v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
   v1.push_back(4);

	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	v1.insert(v1.begin(), 0);
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;


	v1.insert(v1.begin() + 2, 20);
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	int x;
	cin >> x;
	vector<int>::iterator pos = find(v1.begin(), v1.end(), x);
	if (pos != v1.end())
	{
		v1.insert(pos, 1000);
	}
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

}


void test_vector5() //删除
{
	std::vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);
	v1.push_back(5);

	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;
	int x;
	cin >> x;

	vector<int>::iterator it = find(v1.begin(), v1.end(), x);
	if (it != v1.end())
	{
	    //v1.erase(it); //挂掉
		it = v1.erase(it);
		if (it != v1.end()) //不加这句话删除最后一个元素会挂掉
		cout << *it << endl; //帮助我们访问循环内的错误
	}
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;
}

void test_vector6() //删除
{
	std::vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);
	v1.push_back(5);

	std::vector<int>::iterator it = v1.begin();
	while (it != v1.end()) 
	{
		//if (*it % 2 == 0) v1.erase(it); //挂掉了,因为erase之后，it会失效
		if (*it % 2 == 0) it = v1.erase(it);
		else ++it;
	}
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

}

void test_vector7()
{
	std::vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);
	v1.push_back(5);

	std::vector<int> v2(v1);

	for (auto e : v2)
	{
		cout << e << " ";
	}
	cout << endl;
}


void test_vector8()
{
	vector<int> v1 = { 1,2,3,4,5,6 };

}

int main()
{
	test_vector7();

	return 0;
}